{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Egg Drop - SOLUTION\n",
    "This is probably the most common brain teaser riddle out of the group, so really try to think algorithmically about this problem before looking at the solution!\n",
    "\n",
    "## Problem Statement\n",
    "\n",
    "A tower has 100 floors. You've been given two eggs. The eggs are strong enough that they can be dropped from a particular floor in the tower without breaking. You've been tasked to find the highest floor an egg can be dropped without breaking, in as few drops as possible. If an egg is dropped from above its target floor it will break. If it is dropped from that floor or below, it will be intact and you can test drop the egg again on another floor.\n",
    "\n",
    "Show algorithmically how you would go about doing this in as few drops as possible. (Your answer should be a number of the fewest drops needed for testing 2 eggs on 100 floors)\n",
    "## Solution\n",
    "\n",
    "Start from the 10th floor and go up to floors in multiples of 10.\n",
    "\n",
    "If first egg breaks, say at 20th floor then you can check all the floors between 11th and 19th with the second egg to see which floor it will not break.\n",
    "\n",
    "In this case, the worst-case number of drops is 19. If the threshold was 99th floor, then you would have to drop the first egg 10 times and the second egg 9 times in linear fashion.\n",
    "\n",
    "**Best solution:**\n",
    "We need to minimize this worst-case number of drops. For that, we need to generalize the problem to have n floors. What would be the step value, for the first egg? Would it still be 10? Suppose we have 200 floors. Would the step value be still 10? \n",
    "\n",
    "The point to note here is that we are trying to minimize the worst-case number of drops which happens if the threshold is at the highest floors. So, our steps should be of some value which reduces the number of drops of the first egg.\n",
    "\n",
    "Let's assume we take some step value m initially. If every subsequent step is m-1,\n",
    "then, \n",
    "$$m+m−1+m−2+.....+1=n$$\n",
    "\n",
    "This is \n",
    "\n",
    "$$\\frac{m∗(m+1)}{2}=n$$\n",
    "\n",
    "If n =100, then m would be 13.65 which since we can't drop from a decimal of a floor, we actually use 14.\n",
    "\n",
    "So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.\n",
    "\n",
    "Note that this is simply a binary search!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___\n",
    "You can find plenty of other explanations by simply googling \"2 eggs 100 floors\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
